package leetcode.editor.cn;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class P1487MakingFileNamesUnique {
    public static void main(String[] args) {
        Solution solution = new P1487MakingFileNamesUnique().new Solution();
        System.out.println(Arrays.asList(solution.getFolderNames(new String[]{"kaido", "kaido(1)", "kaido", "kaido(1)", "kaido(2)"})));
    }

    Pattern pattern = Pattern.compile("\\([0-9]\\)");

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String[] getFolderNames(String[] names) {
            Map<String, Integer> map = new HashMap<>();
            int length = names.length;
            String[] result = new String[length];
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("(");
            for (int i = 0; i < length; i++) {
                String name = names[i];
                if (map.containsKey(name)) {
                    String str = name;
                    Integer remain = map.get(name);
                    while (map.containsKey(str)) {
                        str = name + "(" + remain++ + ")";
                    }
                    map.put(str, 1);
                    map.put(name, remain );
                    result[i] = str;
                } else {
                    map.put(name, 1);
                    result[i] = name;
                }
            }
            return result;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}